//*****************************************************************************
// 1. Års eksamen @ Datamatiker - EASJ
// Holmegaard Heldagsskole Administrations system 
// 
// Denne klasse er udarbejdet af: Marc og Kasper
//*****************************************************************************
package view;

import control.ControlUtil;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import model.HoldFag;
import model.Personale;

public class PersonaleAdminPanel extends javax.swing.JPanel {

    private ControlUtil controlUtil;
    private DefaultListModel fagList;
    private DefaultListModel persList;
    private boolean rediger;

    public PersonaleAdminPanel(ControlUtil controlUtil) {

        this.controlUtil = controlUtil;
        fagList = new DefaultListModel();
        persList = new DefaultListModel();

        initComponents();

        jList2.setVisible(false);
        jButton1.setVisible(false);

    }

    private void hentPersonale() {
        persList.clear();
        if (vaelgType.getSelectedIndex() >= 0 && vaelgType.getItemCount() > 0) {
            Personale[] personalet = controlUtil.getPersonaleHandler().hentPersonale(vaelgType.getSelectedIndex());
            for (int i = 0; i < personalet.length; i++) {
                persList.addElement(personalet[i]);
            }
            jList1.setEnabled(true);
        }
    }

    private void nulstilGUI() {
        jComboBox2.setSelectedIndex(0);
        jTextField1.setText("");
        jTextField1.setEnabled(false);
        jTextField2.setText("");
        jTextField2.setEnabled(false);
        jButton1.setEnabled(false);
        jButton1.setVisible(false);
        jButton3.setEnabled(false);
        jButton3.setText("Opret");
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList(persList);
        jButton2 = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jComboBox2 = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jButton3 = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        vaelgType = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jList2 = new javax.swing.JList(fagList);

        setLayout(null);

        jList1.setEnabled(false);
        jList1.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                jList1ValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(jList1);

        add(jScrollPane1);
        jScrollPane1.setBounds(40, 80, 220, 330);

        jButton2.setText("Rediger Ansat");
        jButton2.setEnabled(false);
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        add(jButton2);
        jButton2.setBounds(80, 420, 130, 40);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Opret ny ansat"));
        jPanel1.setLayout(null);

        jLabel2.setText("Type:");
        jPanel1.add(jLabel2);
        jLabel2.setBounds(40, 60, 60, 14);

        jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Vælg Type", "Lærer", "Pædagog", "Sekretær", "Administrator" }));
        jComboBox2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox2ActionPerformed(evt);
            }
        });
        jPanel1.add(jComboBox2);
        jComboBox2.setBounds(90, 60, 126, 20);

        jLabel3.setText("Fornavn:    ");
        jPanel1.add(jLabel3);
        jLabel3.setBounds(30, 90, 70, 14);

        jTextField1.setEnabled(false);
        jTextField1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jTextField1KeyReleased(evt);
            }
        });
        jPanel1.add(jTextField1);
        jTextField1.setBounds(90, 90, 120, 20);

        jButton3.setText("Opret");
        jButton3.setEnabled(false);
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton3);
        jButton3.setBounds(80, 180, 100, 40);

        jLabel4.setText("Efternavn:");
        jPanel1.add(jLabel4);
        jLabel4.setBounds(20, 120, 70, 14);

        jTextField2.setEnabled(false);
        jTextField2.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jTextField2KeyReleased(evt);
            }
        });
        jPanel1.add(jTextField2);
        jTextField2.setBounds(90, 120, 120, 20);

        jButton1.setForeground(new java.awt.Color(255, 0, 0));
        jButton1.setText("Slet");
        jButton1.setEnabled(false);
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton1);
        jButton1.setBounds(100, 230, 60, 20);

        add(jPanel1);
        jPanel1.setBounds(650, 50, 250, 270);

        vaelgType.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Vælg personale type", "Lærer", "Pædagog", "Sekretær", "Administrator" }));
        vaelgType.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                vaelgTypeActionPerformed(evt);
            }
        });
        add(vaelgType);
        vaelgType.setBounds(40, 40, 160, 20);

        jLabel1.setText("Vælg type:");
        add(jLabel1);
        jLabel1.setBounds(40, 20, 100, 14);

        jLabel5.setText("Lærerens fag:");
        add(jLabel5);
        jLabel5.setBounds(290, 180, 100, 14);

        jList2.setEnabled(false);
        jScrollPane2.setViewportView(jList2);

        add(jScrollPane2);
        jScrollPane2.setBounds(290, 200, 140, 210);
    }// </editor-fold>//GEN-END:initComponents

    private void vaelgTypeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_vaelgTypeActionPerformed
        if (vaelgType.getSelectedIndex() != 0) {
            hentPersonale();
        } else {
            persList.clear();
        }

    }//GEN-LAST:event_vaelgTypeActionPerformed

    private void jList1ValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_jList1ValueChanged
        if (!jList1.isSelectionEmpty()) {
            jButton1.setEnabled(true);
            jButton2.setEnabled(true);
            fagList.clear();

            if (vaelgType.getSelectedIndex() == 1) {

                jList2.setVisible(true);
                HoldFag[] fagene = controlUtil.getPersonaleHandler().hentPersonensFag((Personale) jList1.getSelectedValue());

                for (int i = 0; i < fagene.length; i++) {
                    fagList.addElement(fagene[i].toString());
                }

            } else {
                jList2.setVisible(false);
            }
        } else {
            jButton1.setEnabled(false);
            jButton2.setEnabled(false);
            jList2.setVisible(false);
        }
    }//GEN-LAST:event_jList1ValueChanged

    private void jComboBox2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox2ActionPerformed
        if (jComboBox2.getSelectedIndex() != 0) {
            jTextField1.setEnabled(true);
        } else {
            jTextField1.setEnabled(false);
        }

    }//GEN-LAST:event_jComboBox2ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed

        String fornavn;
        String efternavn;

        if (rediger) {

            fornavn = jTextField1.getText();
            efternavn = jTextField2.getText();
            Personale pers = (Personale) jList1.getSelectedValue();

            pers.setFornavn(fornavn);
            pers.setEfternavn(efternavn);
            controlUtil.getPersonaleHandler().opdaterPersonale(pers);
            jList1.setEnabled(true);
            vaelgType.setEnabled(true);
            jButton1.setEnabled(true);
            jButton1.setVisible(true);
            rediger = false;

        } else {

            fornavn = jTextField1.getText();
            efternavn = jTextField2.getText();
            char type = (jComboBox2.getSelectedIndex() + "").charAt(0);

            Personale pers = new Personale(fornavn, efternavn, type);
            controlUtil.getPersonaleHandler().nyPersonale(pers);
        }

        nulstilGUI();
        hentPersonale();

    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed

        rediger = true;
        jComboBox2.setEnabled(false);
        jList1.setEnabled(false);
        jButton1.setVisible(true);
        jButton1.setEnabled(true);
        jButton3.setText("Opdater");
        jButton3.setEnabled(true);
        jTextField2.setEnabled(true);
        jButton2.setEnabled(false);
        vaelgType.setEnabled(false);
        Personale pers = (Personale) jList1.getSelectedValue();
        jTextField1.setText(pers.getFornavn());
        jTextField2.setText(pers.getEfternavn());
        jComboBox2.setSelectedIndex(Integer.parseInt(pers.getPtype() + ""));
        jComboBox2.setEditable(false);



    }//GEN-LAST:event_jButton2ActionPerformed

    private void jTextField1KeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField1KeyReleased
        if (jTextField1.getText().length() != 0) {
            jTextField2.setEnabled(true);
        } else {
            jTextField2.setEnabled(false);
        }
    }//GEN-LAST:event_jTextField1KeyReleased

    private void jTextField2KeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField2KeyReleased
        if (jTextField2.getText().length() != 0) {
            jButton3.setEnabled(true);
        } else {
            jButton3.setEnabled(false);
        }

    }//GEN-LAST:event_jTextField2KeyReleased

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed

        Personale pers = (Personale) jList1.getSelectedValue();

        String firstConf = "Er du sikker på du vil slette\n " + jTextField1.getText()
                + " " + jTextField2.getText() + "?";

        String secondConf = "Er du helt sikker på du vil slette\n " + jTextField1.getText()
                + " " + jTextField2.getText() + "?";
        int bekraeft = JOptionPane.showConfirmDialog(this, firstConf);

        if (bekraeft == JOptionPane.YES_OPTION) {
            int slet = JOptionPane.showConfirmDialog(this, secondConf);
            if (slet == JOptionPane.YES_OPTION) {
                controlUtil.getPersonaleHandler().sletPersonale(pers);
                vaelgType.setSelectedIndex(jComboBox2.getSelectedIndex());
                nulstilGUI();

                hentPersonale();

            }
        }
    }//GEN-LAST:event_jButton1ActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JComboBox jComboBox2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JList jList1;
    private javax.swing.JList jList2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JComboBox vaelgType;
    // End of variables declaration//GEN-END:variables
}
